package com.banshou.installs.ui.activity;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;

import com.banshou.installs.R;
import com.banshou.installs.databinding.NearCommBinding;
import com.banshou.installs.model.dao.ResponseZ;
import com.banshou.installs.model.net.Observer;
import com.banshou.installs.model.net.ResponseInfo;
import com.banshou.installs.model.net.RetrofitManager2;
import com.banshou.installs.model.net.RxUtil;
import com.banshou.installs.ui.adapter.ZAdapter;
import com.banshou.installs.utils.DialogUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cjq on 2018/4/19.
 * Email: stoic_yb@139.com
 * features:
 */
public class ZActivity extends BaseActivity {
    private NearCommBinding mBinding;
    private ZAdapter mAdapter;
    private List<ResponseZ.ZList> zList;
    private DialogUtils dialogUtils;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.near_comm);
        initView();
    }

    private void initView() {
        zList = new ArrayList<>();
        dialogUtils = new DialogUtils();
        mBinding.title.pagerTitle.setText("选择幢");
        mBinding.title.back.setOnClickListener(view -> finish());
        mAdapter = new ZAdapter(this, R.layout.nearcomm_item, zList);
        mBinding.recycleview.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        mBinding.recycleview.setAdapter(mAdapter);
        getList();
        getId();
    }

    private void getList() {
        dialogUtils.showAni(this);
        String buildingId = getIntent().getStringExtra("XQID");
        if (buildingId != null) {
            RetrofitManager2.getInstance()
                    .create(ResponseInfo.class)
                    .getZ(buildingId)
                    .compose(RxUtil.lifecycle(this))
                    .subscribe(new Observer<ResponseZ>() {
                        @Override
                        protected void onNextStep(ResponseZ responseZ) {
                            dialogUtils.hideAni();
                            zList.clear();
                            zList.addAll(responseZ.getBuilding_list());
                            mAdapter.notifyDataSetChanged();
                        }

                        @Override
                        protected void onErrorResult() {
                            dialogUtils.hideAni();
                        }
                    });
        }
    }

    private void getId() {
        mAdapter.setOnItemOnClickListener((Id, name) -> {
            Intent intent = new Intent();
            intent.putExtra("ZID", Id);
            intent.putExtra("ZNAME", name);
            setResult(2, intent);
            finish();
        });
    }


}
